package com.company.cloud.common.ribbon.config;

import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;

import com.company.cloud.common.constant.CommonConstant;
import com.company.cloud.common.context.TenantContextHolder;

import cn.hutool.core.util.StrUtil;
import feign.RequestInterceptor;

/**
 * feign拦截器，只包含基础数据
 */
public class FeignInterceptorConfig {
	/**
	 * 使用feign client访问别的微服务时，将上游传过来的client、traceid等信息放入header传递给下一个服务
	 */
	@Bean
	public RequestInterceptor baseFeignInterceptor() {
		return template -> {
			 //传递client
			String tenant = TenantContextHolder.getTenant();
			if (StrUtil.isNotEmpty(tenant)) {
				template.header(CommonConstant.TENANT_HEADER, tenant);
			}
			//传递日志traceId
            String traceId = MDC.get(CommonConstant.LOG_TRACE_ID);
            if (StrUtil.isNotEmpty(traceId)) {
                template.header(CommonConstant.TRACE_ID_HEADER, traceId);
            }
		};
	}

}
